<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if (!defined("joinnumber_lib_include")){
	define("joinnumber_lib_include", 1);

/************************************************************
************************************************************/
	function Getmicrotime () { 
		list($usec, $sec) = explode(" ", microtime()); 
		return ((float)$usec + (float)$sec); 
	}


/************************************************************
************************************************************/
	function callback ($buffer) {
		global $_Charset;
		// replace all the apples with oranges 
		return (preg_replace("/{Charset}/", $_Charset, $buffer));
	} 


/************************************************************
************************************************************/
	function combination ($fatherCount = null, $getOut = null, $getNum = 1, $filter = array(), $getContant = null, $finalArray = null) {

		for($loopCount = 1; $loopCount <= count($fatherCount) && !in_array($loopCount, $filter); $loopCount++) {
			if( $getOut == 1 ){
				$finalArray[] = $fatherCount[$loopCount];
			}elseif ($getOut == $getNum) {
				$finalArray[] = $getContant.'.'.$fatherCount[$loopCount];
			} else {
				$filter[$loopCount] = $loopCount;
				$tempContant = ($getContant ? $getContant.'.'.$fatherCount[$loopCount] : $fatherCount[$loopCount]);
				$finalArray = combination($fatherCount, $getOut, $getNum + 1, $filter, $tempContant, $finalArray);
			}
			unset($filter[$loopCount]);
		}
		return $finalArray;
	}


	function getCombination ($arrChoiceNumber, $intGroupCounter) {
		sort($arrChoiceNumber);
		$arrNum = array();

		if($intGroupCounter == 2){
			for($i1 = 0; $i1 < sizeof($arrChoiceNumber); $i1++){
				$intNum1 = $arrChoiceNumber[$i1];
				for($i2 = 0; $i2 < sizeof($arrChoiceNumber); $i2++){
					$intNum2 = $arrChoiceNumber[$i2];
					if($intNum1 >= $intNum2)continue;
					$arrNum[] = $intNum1.$intNum2;
				}
			}

		}elseif($intGroupCounter == 3){
			for($i1 = 0; $i1 < sizeof($arrChoiceNumber); $i1++){
				$intNum1 = $arrChoiceNumber[$i1];

				for($i2 = 0; $i2 < sizeof($arrChoiceNumber); $i2++){
					$intNum2 = $arrChoiceNumber[$i2];
					if($intNum1 >= $intNum2)continue;

					for($i3 = 0; $i3 < sizeof($arrChoiceNumber); $i3++){
						$intNum3 = $arrChoiceNumber[$i3];
						if($intNum1 >= $intNum3)continue;
						if($intNum2 >= $intNum3)continue;
						$arrNum[] = $intNum1.$intNum2.$intNum3;
					}
				}
			}
		}
		return $arrNum;
	}

/**
 * 組合排碰所有可能號碼組
 * @param	arrChoiceNumber 選號陣列
 * @param	intGroupCounter 幾號為一組
 * @return	array 號碼組合陣列
 */
	function combinationForLine($arrChoiceNumber, $intGroupCounter){
		$arrNum = array();

		if($intGroupCounter == 5){
			foreach($arrChoiceNumber[0] as $intNum1){
				foreach($arrChoiceNumber[1] as $intNum2){
					foreach($arrChoiceNumber[2] as $intNum3){
						foreach($arrChoiceNumber[3] as $intNum4){
							foreach($arrChoiceNumber[4] as $intNum5){
								$arrNum[] = $intNum1.".".$intNum2.".".$intNum3.".".$intNum4.".".$intNum5;
							}
						}
					}
				}
			}
			return $arrNum;
		}

		if($intGroupCounter == 4){
			foreach($arrChoiceNumber[0] as $intNum1){
				foreach($arrChoiceNumber[1] as $intNum2){
					foreach($arrChoiceNumber[2] as $intNum3){
						foreach($arrChoiceNumber[3] as $intNum4){
							$arrNum[] = $intNum1.".".$intNum2.".".$intNum3.".".$intNum4;
						}
					}
				}
			}
			return $arrNum;
		}

		if($intGroupCounter == 3){
			foreach($arrChoiceNumber[0] as $intNum1){
				foreach($arrChoiceNumber[1] as $intNum2){
					foreach($arrChoiceNumber[2] as $intNum3){
						$arrNum[] = $intNum1.".".$intNum2.".".$intNum3;
					}
				}
			}
			return $arrNum;
		}

		foreach($arrChoiceNumber[0] as $intNum1){
			foreach($arrChoiceNumber[1] as $intNum2){
				$arrNum[] = $intNum1.".".$intNum2;
			}
		}
		return $arrNum;
	}


/************************************************************
************************************************************/
	function get_ParlaysNumberEven ($intUID, $intGID, $intBT, $flMaxELimit) {
		global $_Conn, $$_Conn;
		$arrData = array();
		$arrData["isError"] = false;
		$arrData["Number"] = null;

		if (!$intUID || !$intGID || !$intBT || !$flMaxELimit) {
			$arrData["ErrorMessage"] = 'EvenError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven error!!';
			$arrData["isError"] = true;
			return $arrData;
		}

		$strSQL = "SELECT `Number`, SUM(`MemStake`) as MemStake FROM `bet_ParlaysNumberEven` WHERE `MemID` = '$intUID' AND `GambleID` = '$intGID' AND `Bet_Type` = '$intBT' AND isPayOff = 0 group by `Number` having MemStake > '$flMaxELimit'";

		if (!$RS = PMA_mysql_query($strSQL, $$_Conn)) {
			$arrData["ErrorMessage"] = 'EvenError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven SQL error!!';
			$arrData["isError"] = true;
			return $arrData;
		} 
		if(PMA_mysql_num_rows($RS) == 0) {
			$arrData["Number"] = '0';
			return $arrData;
		}

		while ($Row = PMA_mysql_fetch_object($RS)) {
			$arrData[$Row -> Number] = $Row -> MemStake;
		}
		// replace all the apples with oranges
		return $arrData;
	}


/************************************************************
************************************************************/
	function get_ParlaysNumberEven2 ($intUID, $intGID, $intBT, $MaxEven, $JoinArray, $OffItemsSeries) {
		global $_Conn, $$_Conn;
		$arrData = array();
		$arrData["isError"] = false;
		$arrData["Number"] = null;

		if(!$intUID || !$intGID || !$intBT || !$MaxEven || !is_array($JoinArray)){
			$arrData["ErrorMessage"] = 'EvenError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven2 error!!';
			$arrData["isError"] = true;
			return $arrData;
		}

		$strSQL = "SELECT `Number` FROM `bet_ParlaysNumberEven` WHERE `MemID` = '$intUID' AND `GambleID` = '$intGID' AND `Bet_Type` = '$intBT' AND `MemStake` > '$MaxEven' AND isPayOff = 0 AND `Number` IN(";
		
		foreach ($JoinArray as $value) {
			$Number_Value = null;
			/*$JoinNumber	= explode('.', $value);
			sort($JoinNumber);
			foreach ($JoinNumber as $arrValue) {
				$Number_Value .= $arrValue;
			}*/

			$strSQL_In .= "'$Number_Value',";
		}

		$strSQL	.= substr($strSQL_In, 0, -1). ')';

		if (!$RS = PMA_mysql_query($strSQL, $$_Conn)) {
			$arrData["ErrorMessage"] = 'EvenError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven2 SQL error!!';
			$arrData["isError"] = true;
			return $arrData;
		}
		if(PMA_mysql_num_rows($RS) == 0) {
			$arrData[Number] = '0';
			return $arrData;
		}


		while ($Row = PMA_mysql_fetch_object($RS)) {
			$arrData["Number"] .= chunk_split($Row -> Number, 2, $OffItemsSeries);
			$arrData["Number"] = substr($arrData["Number"], 0, -2).'<br>';
		}
		// replace all the apples with oranges
		return $arrData;
	}


/************************************************************
************************************************************/
	function get_ParlaysNumberEvenGap ($intGID, $intBT, $arrJoinNum) {
		global $_Conn, $$_Conn, $_KSysParam;

		$isRealJumpWager = $_KSysParam["isRealJumpWager"];
		$arrData = array();
		$arrData["isError"] = false;
		$arrData["Number"] = null;

		if (!$intGID || !$intBT) {
			$arrData["ErrorMessage"] = 'EvenGapError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven error!!';
			$arrData["isError"] = true;
			return $arrData;
		}

		$arrJNum = array();
		foreach ($arrJoinNum as $strNumber) {
			$strNumber	= str_replace('.', '', $strNumber);
			$strNumber = sprintf("%010s", $strNumber);
			$arrJNum[] = $strNumber;
		}

		if($isRealJumpWager == 0){
			$strSQL = "select `Number`, SUM(`MemStake`) as MemStake from `bet_ParlaysNumberEven` where `GambleID` = '".$intGID."' and `Bet_Type` = '".$intBT."' and `isPayOff` = 0 and `Number` in (".implode(",", $arrJNum).") group by `Number`";
		}else{
			$strSQL = "select a.Number, SUM(a.MemStake*b.DirectorRate*b.MemExchangeRate) as MemStake from `bet_ParlaysNumberEven` a, `bet_BetListTmp` b where b.GambleID = '".$intGID."' and b.Bet_Type = '".$intBT."' and b.isPayOff = 0 and a.BetListID = b.ID and a.Number in (".implode(",", $arrJNum).") group by a.Number";
		}
		//print $strSQL;

		if (!$RS = PMA_mysql_query($strSQL, $$_Conn)) {
			$arrData["ErrorMessage"] = 'EvenGapError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven SQL error!!';
			$arrData["isError"] = true;
			return $arrData;
		} 
		if(PMA_mysql_num_rows($RS) == 0) {
			$arrData["Number"] = '0';
			return $arrData;
		}
		while ($Row = PMA_mysql_fetch_object($RS)) {
			$arrData[$Row -> Number] = $Row -> MemStake;
		}
		//print_r($arrData);
		// replace all the apples with oranges
		return $arrData;
	}


/************************************************************
************************************************************/
	function get_BetListStake ($intUID, $intGID, $intBT) {
		global $_Conn, $$_Conn;
		$arrData = array();
		$arrData["isError"] = false;

		if (!$intUID || !$intGID || !$intBT) {
			$arrData["ErrorMessage"] = 'EvenError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven error!!';
			$arrData["isError"] = true;
			return $arrData;
		}

		$strSQL = "SELECT SUM(`MemStake`) AS MemStake FROM `bet_BetList` WHERE `MemID` = '$intUID' AND `GambleID` = '$intGID' AND `Bet_Type` = '$intBT'  AND isPayOff = 0 ";

		if (!$RS = PMA_mysql_query($strSQL, $$_Conn)) {
			$arrData["ErrorMessage"] = 'EvenError';
			$arrData["ErrorHiddenMessage"] = 'Error: Function get_ParlaysNumberEven SQL error!!';
			$arrData["isError"] = true;
			return $arrData;
		}
		if(PMA_mysql_num_rows($RS) == 0) {
			$arrData["MemStake"] = 0;
			return $arrData;
		}

		$Row = PMA_mysql_fetch_object($RS);
		$arrData["MemStake"] = $Row -> MemStake;

		// replace all the apples with oranges
		return $arrData;
	}


/**
 * 取的連碼單一號碼排行榜資料 XML 型態
 * @param	intGID 操盤序號
 * @param	intSortNo 回傳排行榜數量
 * @param	isRealShow 是否顯示實貨
 * @param	intUserID 使用者序號
 * @param	strPower 權限種類
 * @return	false -> 失敗, Array[?x][?y] = ?z -> 成功
 *			?x -> 下注種類
 *			?y -> 號碼
 *			?z -> 累積注額(實貨)
 */
	function getJNSortData($intGID, $intSortNo, $isRealShow = 1, $intUserID = 0, $strPower = "") {
		global $_Conn, $$_Conn;

		$arrBT = array(7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array());
		$arrSort = array(7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array());
		$arrSQL = array();
		switch($strPower){
			case "sub":						//顯示該代理商全部及時注單
				$strSeach = " and b.SubID = ".$intUserID." ";
				$strN = ($isRealShow == 1)?"*IF(b.WagerType = 2,-1,1 - b.SubRate)":"";
				break;

			case "main":					//顯示該總代理全部及時注單
				$strSeach = " and b.MainID = ".$intUserID." ";
				$strN = ($isRealShow == 1)?"*IF(b.WagerType = 3,-1,b.SubRate - b.MainRate)":"";
				break;

			case "stock":					//顯示該股東全部及時注單
				$strSeach = " and b.StockID = ".$intUserID." ";
				$strN = ($isRealShow == 1)?"*IF(b.WagerType = 4,-1,b.MainRate - b.StockRate)":"";
				break;

			case "director":				//顯示該總監全部及時注單
				$strSeach = " and b.DirectorID = ".$intUserID." ";
				$strN = ($isRealShow == 1)?"*IF(b.WagerType = 5,-1,b.StockRate - b.DirectorRate)":"";
				break;

			default:
				$strSeach = "";
				$strN = ($isRealShow == 1)?"*ROUND(b.DirectorRate, 4)":"";
				break;
		}


		for($i = 1; $i <= 3; $i++){
			$arrSQL[$i] = "(select a.Bet_Type, a.Ball$i as Ball, SUM(a.MemStake*ROUND(b.MemExchangeRate, 4)".$strN.") as MemStake from `bet_ParlaysNumberEven` a, `bet_BetListTmp` b where a.GambleID = '".$intGID."' and a.Ball$i <> 0 and a.isPayOff = 0 and a.BetListID = b.ID ".$strSeach." group by a.Bet_Type, a.Ball$i)";
		}

		if (!$RS = PMA_mysql_query(implode(" union ", $arrSQL), $$_Conn)) {
			$GLOBALS["errormessage"] = "Find NumberEven Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}
		while($Row = PMA_mysql_fetch_object($RS)){
			if($Row -> Ball == -1)continue;
			$arrBT[$Row -> Bet_Type][$Row -> Ball] += round($Row -> MemStake);
		}


		for($i = 7; $i < 12; $i++){
			arsort($arrBT[$i]);
			$j = 0;
			foreach($arrBT[$i] as $intNum => $intVal){
				$arrSort[$i][$j] = $intNum."@".$intVal;
				$j++;
				if($j == $intSortNo)break;
			}
		}
		return $arrSort;
	}
//-------------------------------------------------------//
//joinnumber_lib End
}
?>